package com.orangelife.mobile.surrounding.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.util.OrangePhoneUtil;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.IntentHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.activities.adapter.ActivityDetailListViewAdapter;
import com.common.jump.CommonJumper;
import com.orangelife.mobile.surrounding.entity.GoodsEntity;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.shoppingcart.biz.ShoppingCartBiz;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.widget.ScrollListView;
import com.orangelife.mobile.widget.imagegridview.ImageGridView;

import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yyx
 * on 2015/10/16.
 */
public class CommercialDetailActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_call)
    ImageView ivTitleCall;
    @Bind(R.id.iv_title_shopping_cart)
    ImageView ivTitleShoppingCart;
    @Bind(R.id.tvGoodNumber)
    TextView tvGoodNumber;
    @Bind(R.id.flShoppingCart)
    FrameLayout flShoppingCart;
    @Bind(R.id.ll_score)
    LinearLayout llScore;
    @Bind(R.id.rl_title)
    RelativeLayout rlTitle;
    @Bind(R.id.ivCommercial)
    ImageView ivCommercial;
    @Bind(R.id.tvCommercial)
    TextView tvCommercial;
    @Bind(R.id.slList)
    ScrollListView slList;
    @Bind(R.id.tvFirst)
    TextView tvFirst;
    @Bind(R.id.viewFirst)
    View viewFirst;
    @Bind(R.id.ll_submit_form)
    LinearLayout llSubmitForm;
    @Bind(R.id.tvSecond)
    TextView tvSecond;
    @Bind(R.id.viewSecond)
    View viewSecond;
    @Bind(R.id.ll_history_record)
    LinearLayout llHistoryRecord;
    @Bind(R.id.tvAddress)
    TextView tvAddress;
    @Bind(R.id.ivSendRange)
    ImageView ivSendRange;
    @Bind(R.id.gvPhoto)
    ImageGridView gvPhoto;
    @Bind(R.id.tvSendRange)
    TextView tvSendRange;

    private Map<String, Object> map = null;
    private ImageDownloader image = ImageDownloader.getImageDownloader();
    /** 经度 */
    private float longitude;
    /** 纬度 */
    private float latitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circumtesco_commercialdetail);
        ButterKnife.bind(this);
        findView();
    }

    /**
     * 赋值
     */
    private void findView() {
        map = GoodsEntity.getMap();
        llSubmitForm.setOnClickListener(listener);
        tvAddress.setOnClickListener(listener);
        flShoppingCart.setOnClickListener(listener);
        ivTitleCall.setOnClickListener(listener);
        setDetail();
        ToastHelper.getInstance().init(this);
    }

    protected void onResume() {
        super.onResume();
        if (getGoodsNumber() == 0) {
            tvGoodNumber.setVisibility(View.GONE);
        } else {
            tvGoodNumber.setVisibility(View.VISIBLE);
            tvGoodNumber.setText(getGoodsNumber() + "");
        }
    }

    /**
     * 设置详情
     */
    private void setDetail() {

        tvFirst.setText("商品列表");
        tvSecond.setText("商户详情");
        setColor();
        if (map != null && map.size() != 0) {
            latitude = Float.parseFloat(map.get("latitude").toString());
            longitude = Float.parseFloat(map.get("longitude").toString());
            tvTitle.setText(map.get("merchantName").toString());
            tvCommercial.setText(map.get("merchantName").toString());
            String url = map.get("merchantLogo").toString();
            image.download(url, ivCommercial, ImageDownloader.ImageSize.IMAGE_SIZE_EXTRA_SMALL_SQUARE_JPG);
            List<Map<String, Object>> listSummary = (List<Map<String, Object>>) map.get("summary");
            ActivityDetailListViewAdapter adapter = new ActivityDetailListViewAdapter(
                    this, listSummary);
            slList.setAdapter(adapter);

            tvAddress.setText(map.get("stree").toString());
            String imgUrl = map.get("dispatchingImg").toString();
            if (StringUtil.isBlank(imgUrl)) {
                tvSendRange.setVisibility(View.GONE);
                ivSendRange.setVisibility(View.GONE);
            } else {
                image.download(imgUrl, ivSendRange, ImageDownloader.ImageSize.IMAGE_SIZE_MIDDLE_JPG);
            }
            String pics = map.get("merchantImgs").toString();
            gvPhoto.init(this, pics, "##");
        }
    }

    private void setColor() {
        tvSecond.setTextColor(Color.parseColor("#FDA110"));
        viewSecond.setVisibility(View.VISIBLE);
        tvFirst.setTextColor(Color.parseColor("#000000"));
        viewFirst.setVisibility(View.INVISIBLE);
    }

    /**
     * 获取购物车数量
     *
     * @return
     */
    private int getGoodsNumber() {
        return ShoppingCartBiz.getGoodsCount();
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.ll_submit_form:
                    finish();
                    GoodsEntity.setIsClick(true);
                    IntentHelper.getInstances().getIntent(CommercialDetailActivity.this, CommercialListActivity.class);
                    break;
                case R.id.tvAddress:
                    CommonJumper.jumpToBaiduLocation(CommercialDetailActivity.this, longitude, latitude);
                    break;
                case R.id.iv_title_call:
                    //打电话
                    OrangePhoneUtil.callCustomerService2(CommercialDetailActivity.this);
                    break;
                case R.id.flShoppingCart:
                    CommonJumper.jumpToShoppingCart(CommercialDetailActivity.this);
                    finish();
                    break;
                default:
                    break;
            }

        }
    };
}
